﻿/*
 * 设计模式 - 迭代器模式
 * 目的: 循环遍历输出结果
 */

using _12_迭代器模式;

var users = new List<User>()
{
    new User(){Name = "张三"},
    new User(){Name = "李四"},
    new User(){Name = "王武"},
    new User(){Name = "马六"},
    new User(){Name = "孙奇"},
};

var collection = new UserCollection(users.ToArray());

while (collection.HasNext())
{
    var user = (User)collection.Next();
    Console.WriteLine(user.Name);
}

Console.WriteLine("微软自带迭代模式:");

Other.Main main = new Other.Main();
main.Show();

/// <summary>
/// 标准迭代器接口
/// 一定会有两个方法  HasNext  和  Next
/// </summary>
interface IIterator
{
    bool HasNext();

    object Next();
}


public class UserCollection : IIterator
{
    User[] _users;
    private int index;

    public UserCollection(User[] users)
    {
        this._users = users;
    }
    public bool HasNext()
    {
        return index < _users.Length;
    }

    public object Next()
    {
        var instance = _users[index++];
        return instance;
    }
}


